// Uncomment these imports to begin using these cool features!

import {service} from '@loopback/core';
import {Count, repository} from '@loopback/repository';
import {get, getModelSchemaRef, param, post, requestBody, response} from '@loopback/rest';
import {Employ} from '../models';
import {EmployRepository} from '../repositories';
import {EmployService} from '../services';

// import {inject} from '@loopback/core';

// 需要token验证的时候打开
// import {authenticate} from '@loopback/authentication';
// @authenticate('jwt')
export class EmpoyController {
  constructor(
    @repository(EmployRepository)
    public employRepository: EmployRepository,
    @service(EmployService) public employService: EmployService,
  ) { }

  // XXX: 新增区域

  // 创建新的数据
  @post('/createEmpoly', {
    summary: 'create data',
    responses: {
      '200': {
        description: '返回信息创建的情况',
        content: {
          'application/json': {
            schema: {
              type: 'array',
              items: getModelSchemaRef(Employ, {
                includeRelations: true,
              }),
            },
          },
        },
      },
    },
  })
  async createEmpoly(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Employ),
          examples: {
            '新增的信息：': {
              summary: '创建1',
              description: `描述`,
              value: {
                employeeNo: 'employee_no',
                name: 'name++',
                team: 0,
              },
            },
          },
        },
      },
    })
    employ: Employ,
  ): Promise<Employ> {
    return this.employService.createEmploy(employ);
  }

  // XXX: 查询区域

  // 查询数据 写法1
  @post('/findEmploy', {
    summary: 'test data',
    responses: {
      '200': {
        description: '返回信息',
        content: {
          'application/json': {
            schema: {
              type: 'array',
              items: getModelSchemaRef(Employ, {
                includeRelations: true,
              }),
            },
          },
        },
      },
    },
  })
  public async findEmploy(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Employ),
          examples: {
            '查询人员信息1: ': {
              summary: '查询人员信息1',
              description: `api的描述信息,获取信息0.0`,
              value: {
                employeeNo: '插入1100',
                name: '0.0',
              },
            },
            '查询人员信息2: ': {
              summary: '查询人员信息2',
              description: `api的描述信息,获取信息 string`,
              value: {
                employeeNo: 'string70EMP2',
                name: 'string',
              },
            },
          },
        },
      },
    })
    employ: Employ,
  ): Promise<Employ[]> {
    return this.employService.findEmploy(employ);
  }

  // get 查询测试 写法2
  @get('/findEmployByGet')
  @response(200, {
    description: 'Array of MounterProgram model instances',
    content: {
      'application/json': {
        schema: {
          type: 'Object',
          example: { // 例子，需要传什么
            "employeeNo": "employeeNo",
            "name": "name",
            "team": "team",
          },
          properties: {  // 指定类型
            "employeeNo": {type: "string"},
            "name": {type: "string"},
            "team": {type: "number"},
          }
        },
      },
    },
  })
  async findEmployByGet(
    @param.query.string('employeeNo', {required: true, example: 'employee_no'}) employeeNo: string,   // 只需要传进来的哪里一个数据
  ): Promise<Employ[]> {
    return this.employService.findEmployByGet(employeeNo);
  }

  // XXX: 更新区域

  // 更新数据
  @post('/updateEmploy', {
    summary: 'updateEmploy data',
    responses: {
      '200': {
        description: '返回信息更新的情况',
        content: {
          'application/json': {
            schema: {
              type: 'array',
              items: getModelSchemaRef(Employ, {
                includeRelations: true,
              }),
            },
          },
        },
      },
    },
  })
  async updateEmploy(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Employ),
          examples: {
            '更新的信息：': {
              summary: '更新1',
              description: `描述`,
              value: {
                employeeNo: 'employee_no',
                name: 'name+dsadadadd+',
              },
            },
          },
        },
      },
    })
    employ: Employ,
  ): Promise<Count> {
    // const data = this.employRepository.update();
    // console.log(data);
    return this.employService.updateEmploy(employ, {
      employeeNo: employ.employeeNo,
    });
  }

  // XXX: 删除区域

  // 删除数据
  @post('/delectEmploy', {
    summary: 'delectEmploy data by employee_no',
    responses: {
      '200': {
        description: '返回信息删除的情况',
        content: {
          'application/json': {
            schema: {
              type: 'array',
              items: getModelSchemaRef(Employ, {
                includeRelations: true,
              }),
            },
          },
        },
      },
    },
  })
  async delectEmploy(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Employ),
          examples: {
            '删除的信息：': {
              summary: '删除1',
              description: `描述`,
              value: {
                employeeNo: 'employee_no',
              },
            },
          },
        },
      },
    })
    employ: Employ,
  ): Promise<Count> {
    return this.employService.delectEmploy({
      employeeNo: employ.employeeNo,
    });
  }
}
